export function downloadFile(data: Blob, filename: string, type: string = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
  // 创建 Blob 对象
  const blob = new Blob([data], { type: type });
  // 判断当前浏览器是否是IE，由于IE是没有download 方法的，需要用msSaveBlob() 或 msSaveOrOpenBlob()
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    // 兼容IE
    window.navigator.msSaveOrOpenBlob(blob, filename);
  } else {
    // 创建 URL 对象
    const url = URL.createObjectURL(blob);
    // 创建链接
    const link = document.createElement('a');
    link.href = url;
    link.download = filename;
    // 模拟点击链接进行下载
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    // 释放 URL 对象
    URL.revokeObjectURL(url);
  }
}

export function isBlob(val: Blob) {
  return toString.call(val) === '[object Blob]';
};

export function formData(param: object | any) {
  let data = new FormData();
  for (const key in param) {
    data.append(key, param[key]);
  }
  return data;
}

/**
 * 表格时间格式化
 */
export function formatDateTime(cellValue: any) {
  if (cellValue == null || cellValue == "") return "";
  var date = new Date(cellValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}

/**
 * 表格时间格式化
 */
export function formatDate(cellValue: any) {
  if (cellValue == null || cellValue == "") return "";
  var date = new Date(cellValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return year + '-' + month + '-' + day;
}

export function dedup(arr: any, code: string) {
  for (let i = 0; i < arr.length; i++) {
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[i][code] == arr[j][code]) {
        arr.splice(j, 1);
        j--;
      }
    }
  }
  return arr;
}